package com.zmytest.util;

import org.springframework.util.StringUtils;

import javax.crypto.KeyGenerator;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created on 2018/12/21.
 *
 * @author zhangmingyu
 */
public class Encryptor {

    public static String getSHACode(String value) {
        return getCode(value,"SHA");
    }

    public static String getMd5Code(String value) {
        return getCode(value,"MD5");
    }

    private static String getCode(String value,String algorithm) {
        try {
            byte[] inputs = value.getBytes();
            MessageDigest digest = MessageDigest.getInstance(algorithm);
            digest.update(inputs);
            byte[] resultBytes = digest.digest();
            return new BigInteger(1,resultBytes).toString(16);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}
